﻿using ThoughtWorks.CruiseControl.Core.Tasks.Conditions;
using Exortech.NetReflector;
using NUnit.Framework;

namespace ThoughtWorks.CruiseControl.UnitTests.Core.Tasks.Conditions
{
    [TestFixture]
    public class orTaskConditionTest
    {
        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
        }

        private void executeTest(bool conditionResult1, bool conditionResult2, bool expected)
        {
            bool result = buildTestObject(conditionResult1, conditionResult2).Eval(null);
            string message = string.Format("Compared '{0}' to '{1}' and expected '{2}'", conditionResult1, conditionResult2, expected);
            Assert.That(result, Is.EqualTo(expected), message);
        }


        private OrTaskCondition buildTestObject(params bool[] results)
        {
            OrTaskCondition result = new OrTaskCondition();

            result.Conditions = TaskConditionMock.GetMockConditions(results);

            return result;
        }

        [Test]
        [Description("BulkTest Pass")]
        [Category("Bulk Test")]
        public void orTaskConditionTest_BulkTest_Pass()
        {
            executeTest(true, true, true);
            executeTest(true, false, true);
            executeTest(false, true, true);
            executeTest(false, false, false);
        }

        [Test]
        [Description("XmlInterface Pass")]
        [Category("passing test")]
        public void orTaskConditionTest_XmlInterface_Pass()
        {
            string testXml = "<orCondition><conditions><mock result=\"true\"/><mock result=\"false\"/></conditions></orCondition>";

            NetReflectorTypeTable table = new NetReflectorTypeTable();
            table.Add(typeof (OrTaskCondition));
            table.Add(typeof (TaskConditionMock));
            OrTaskCondition result = NetReflector.Read(testXml, table) as OrTaskCondition;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Conditions.Length, Is.EqualTo(2));
            Assert.That(result.Conditions[0].Eval(null), Is.EqualTo(true));
            Assert.That(result.Conditions[1].Eval(null), Is.EqualTo(false));
            Assert.That(result.Eval(null), Is.EqualTo(true));
        }

        [Test]
        [Description("emptyConditions EvalsToFalse")]
        [Category("Category")]
        [ExpectedException(typeof(System.NullReferenceException))]
        public void orTaskConditionTest_emptyConditions_EvalsToFalse()
        {
            OrTaskCondition taskCondition = new OrTaskCondition();
            bool condition = taskCondition.Eval(null);
            Assert.That(condition, Is.False);
        }
    }
}